package bz.ziro.kanbe.controller.mng.comment;

import java.util.Date;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import bz.ziro.kanbe.dao.CommentDao;
import bz.ziro.kanbe.model.CommentConfig;
import bz.ziro.kanbe.util.KeyFactory;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * コメント設定更新
 * @author Administrator
 *
 */
public class UpdateConfigController extends Controller {

    @Override
    public Navigation run() {
 
        String siteKeyBuf = sessionScope("siteKey");
        String templateKeyBuf = requestScope("templateKey");

        //ページのキーを取得
        Key key = KeyFactory.createTemplateKey(Long.valueOf(siteKeyBuf),Long.valueOf(templateKeyBuf));
        //コメント設定を取得
        CommentConfig config = CommentDao.findConfig(key);

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if ( config == null ) {

        	config = new CommentConfig();
        	Key configKey = KeyFactory.allocateCommentConfigKey();
        	config.setKey(configKey);
        	config.setOwnerTemplateKey(key);

        	//作成日付
        	config.setCreateDate(new Date());
        	config.setCreator(user);

        } else {
        	//更新日付
        	
        	config.setEditDate(new Date());
        	config.setEditor(user);
        }
        
        BeanUtil.copy(request, config);
        Datastore.put(config);
        return null;
    }
}
